<template>
  <transition name="el-fade-in-linear">
    <router-view />
  </transition>
</template>

<script>
import Cookies from 'js-cookie';
import { messages } from '@/i18n';
import langs from '@/i18n/langs';
// import webSocket from '@/components/webSocket/index.vue';
export default {
  watch: {
    '$i18n.locale': 'i18nHandle',
    $route(to, from) {
      // this.checkOwnerSatate();
    },
  },
  data() {
    return {
      language: '',
      // 定义校验所有者状态定时器
      ownerSatateTime: null,
      alerts: [],
      alert_dissolution: [],
    };
  },
  components: {
    
    // webSocket,
  },
  created() {
    /***
     * 设置浏览器标签栏的图片1112
     *
     */
    this.changeFavicon();

    this.i18nHandle(this.$i18n.locale);

    window.addEventListener('storage', () => {
      if (window.location.href.indexOf('login') === -1) {
        this.$message({
          message: '当前身份已失效',
          type: 'warning',
        });
        location.reload();
      }
    });

    // 校验当前账号下的当前机构所有者状态是否变化
    // 初始化国际化
    this.initLanguage();
    // this.checkOwnerSatate();
  },
  methods: {
    initLanguage() {
      let storageLang = localStorage.getItem('lang');
      if (storageLang === 'cn') {
        this.language = 'cn';
      } else if (storageLang === 'en') {
        this.language = 'en';
      } else if (storageLang === 'pt') {
        this.language = 'pt';
      } else {
        let language = navigator.language || navigator.userLanguage;

        if (language === 'zh-CN' || language === 'zh-TW') {
          this.language = 'cn';
        } else if(language === 'pt' || language === 'pt-BR' || language === 'pt-PT'){
          this.language = 'pt';
        } else {
          this.language = 'en';
        }
      }

      this.languageHandle(this.language);
    },
    languageHandle(type) {
      this.language = type;
      localStorage.setItem('lang', type);
      this.$i18n.locale = type;
      let baseTitle = messages[type]['brand'][this.$config.labelText];
      let prefixTitle = langs[type]['login']['denglu'];
      document.title = prefixTitle + '-' + baseTitle;
    },
    // 判断是否出现弹框
    judgeShowBullet() {
      let user_id = window.localStorage.getItem('user_id');
      let deptId = window.localStorage.getItem('user_jg_id');
      let user_superAdmin = window.localStorage.getItem('user_superAdmin');

      let user_owner = window.localStorage.getItem('user_owner');
      let ignoreArrs = [
        '/login',
        '/threeLoad',
        '/register',
        '/selectionregister',
        '/selectionnextregister',
        '/mobileTerminal',
      ];
      if (ignoreArrs.indexOf(this.$route.path) !== -1) {
        return false;
      }
      if (user_superAdmin === '1') {
        return false;
      }
      if (!user_owner || user_owner === 'null') {
        return false;
      }
      if ((user_id && user_id == 1) || !user_id || user_id === 'null') {
        return false;
      }
      if (!deptId) {
        return false;
      }

      this.$http
        .get(`/sys/organized/authentication?userId=${user_id}&deptId=${deptId}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            if (res.data.Owner && Number(user_owner) != res.data.Owner) {
              // 说明状态改变了
              let alert = this.$alert(
                this.$t('members.chengyuan77'),
                this.$t('members.chengyuan38'),
                {
                  confirmButtonText: this.$t('members.chengyuan31'),
                  callback: (action) => {
                    this.logOut();
                    if (this.alerts && this.alerts.length > 0) {
                      this.alerts.forEach((item) => {
                        item.close();
                      });
                    }
                  },
                },
              );
              this.alerts.push(alert);
            } else {
              if (this.alerts && this.alerts.length > 0) {
                this.alerts.forEach((item) => {
                  item.close();
                });
              }
            }
            //
            if (res.data.CurrentLoginStatus === false) {
              let alert_dissolution = this.$alert(
                `${this.$t('members.chengyuan79')}
                  ${res.data.OwnerName}${this.$t('members.chengyuan80')}
                  【${res.data.OwnerDeptName}】
                  ${this.$t('members.chengyuan81')}`,
                this.$t('members.chengyuan38'),
                {
                  confirmButtonText: this.$t('members.chengyuan31'),
                  callback: (action) => {
                    this.logOut();
                    if (this.alert_dissolution && this.alert_dissolution.length > 0) {
                      this.alert_dissolution.forEach((item) => {
                        item.close();
                      });
                    }
                  },
                },
              );
              this.alert_dissolution.push(alert_dissolution);
            } else {
              if (this.alert_dissolution && this.alert_dissolution.length > 0) {
                this.alert_dissolution.forEach((item) => {
                  item.close();
                });
              }
            }
          }
        })
        .catch((err) => {
          console.log('err', err);
        })
        .finally(() => {
          clearTimeout(this.ownerSatateTime);
          this.ownerSatateTime = setTimeout(this.judgeShowBullet, 1000 * 10);
        });
    },
    checkOwnerSatate() {
      this.ownerSatateTime = setTimeout(this.judgeShowBullet, 1000 * 20);
    },

    logOut() {
      this.$http
        .post('/open/logout')
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          window.localStorage.removeItem('user_headUrl');
          window.localStorage.removeItem('user_realName');
          window.localStorage.removeItem('user_superAdmin');
          window.localStorage.removeItem('user_organizedRoleId');
          window.localStorage.removeItem('user_organizedType');
          window.localStorage.removeItem('user_roleId');
          window.localStorage.removeItem('user_jg_id');
          window.localStorage.removeItem('user_owner');
          window.localStorage.removeItem('user_permissionsList');
          window.localStorage.removeItem('user_id');
          window.localStorage.removeItem('user_name');
          window.localStorage.removeItem('user_roleId');
          window.localStorage.removeItem('user_organizedType');
          window.localStorage.removeItem('user_adminRoleName');
          window.localStorage.removeItem('tg_expand');
          // window.localStorage.removeItem('lang');
          Cookies.set('token', '', -1);

          this.$router.push('/');
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    changeFavicon() {
      var linkElement = document.querySelector('link[rel="shortcut icon"]'); // "myLink"为链接元素的id属性值
      linkElement.href = this.$config.iconPath;
    },
    i18nHandle(val, oldVal) {
      Cookies.set('language', val);

      document.querySelector('html').setAttribute('lang', val);

      // 非登录页面，切换语言刷新页面
      if (this.$route.name !== 'login' && oldVal) {
        // window.location.reload();
      }
    },
  },
};
</script>
<style lang="scss">
.el-table th.gutter {
  display: table-cell !important;
}

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

html,
body,
#app {
  width: 100%;
  height: 100%;
}
</style>
